<?php
namespace app\pachong\controller;  // 必须继承于app 第二个是具体的应用名  例 t5/note/tp
use think\Controller;
//use think\Request;


use think\config;


// use think\Db;
class Index extends Controller
{
/*
*https://www.hyhthjk.com.cn/note.php/python/Index/index
django 的安装
*/

public function index(){
echo'
<pre>
地址 http://www.hyhthjk.com.cn/note.php/pachong/Index/index  爬虫
</pre>
';
}


/***
 * 通过fopen 下载页面
 */
public function index2(){
  $this->index5();
  //  http://www.hyhthjk.com.cn/note.php/pachong/Index/index2/"http://www.10jqka.com.cn/"
    //以只读方式‘r’ 打开一个url
    $url = "http://www.10jqka.com.cn/";
    $content = "";
  $f = fopen($url,'r');
  echo $url.'<br/>';
  if($f){
      //一次读取102400字节 直到没有的可读取
      while(($buffer = fgets($f,102400)) !== false){
          
           //读取出的文件 都存入$content
           $content = $content.$buffer;
      }
      fclose($f);
  } 
  return $content;
}

/*
*通过 file_get_contents 下载页面
*/
public function index3(){
    $this->index6();
    $content = file_get_contents('http://www.kanguba.com/chigu/601318');
    return $content;
}


/*
*通过 curl 下载页面
*/
public function index4(){
    $this->index7();
   // $ch = curl_init('http://data.eastmoney.com/gpzy/detail/300395.html');
   $ch = curl_init('http://data.cnstock.com/gpsj/ggcg/600600.html');
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
    $content = curl_exec($ch);
    curl_close($ch);
    echo $content ;
    echo"完毕";
    //return $content;

}

public function index5(){
    echo'
    <pre>
    //  http://www.hyhthjk.com.cn/note.php/pachong/Index/index2/"http://www.10jqka.com.cn/"
    //以只读方式‘r’ 打开一个url
    $url = "http://www.10jqka.com.cn/";
    $content = "";
  $f = fopen($url,"r");
  if($f){
      //一次读取102400字节 直到没有的可读取
      while(($buffer = fgets($f,102400)) !== false){
          
           //读取出的文件 都存入$content
           $content = $content.$buffer;
      }
      fclose($f);
  } 
  return $content;
  <hr>
    </pre>
    ';

}

public function index6(){
echo'
<pre>
$content = file_get_contents("http://www.iwencai.com/stockpick/search?tid=stockpick&qs=sl_box_main_ths&w=601318");
return $content;

</pre>
';
}


public function index7(){
  echo'
  <pre>
  $ch = curl_init("http://data.eastmoney.com/gpzy/detail/300395.html");
  curl_setopt( $ch,CURLOPT_HEADER,0);
  curl_setopt( $ch,CURLOPT_RETURNTRANSFER,0);
  $content = curl_exec($ch);
  curl_close($ch);
  return $content;

  <pre><hr/>
  ';
}

public function index8(){
    $this->index9();
    $stock_string="http://hq.sinajs.cn/list=sh601318";
			$stock_string=file_get_contents($stock_string);
			 $stock_string = substr($stock_string,21,-3);
			 $stock_string = explode(",",$stock_string);
			$name = $stock_string[0];
			 $data["name"]=mb_convert_encoding($name,"UTF-8","GB2312");
			
			  $map["daima"]="601318";
			
			   $data["zuoshoupan"]=$stock_string[2];
			$data["dangqianjia"]=$stock_string[3];
			$data['vol']=$stock_string[8];
			//  $data['vol'] = $vol+1;
			$data['date']=$stock_string[30];
            $data['time']=$stock_string[31];
            dump($data);
}

public function index9(){
echo'
<pre>
$stock_string="http://hq.sinajs.cn/list=sh601318";
$stock_string=file_get_contents($stock_string);
 $stock_string = substr($stock_string,21,-3);
 $stock_string = explode(",",$stock_string);
$name = $stock_string[0];
 $data["name"]=mb_convert_encoding($name,"UTF-8","GB2312");

  $map["daima"]="601318";

   $data["zuoshoupan"]=$stock_string[2];
$data["dangqianjia"]=$stock_string[3];


</pre>
<hr/>
';

}

public function index10(){
    return $this->fetch();

}

/***
 * 正则匹配
 */
public function index11(){
    $this->index12();
    $content = file_get_contents("http://www.hyhthjk.com.cn/note.php/pachong/Index/index10");
    $matches = array();
    //要匹配的内容  斜杠 / /是匹配的开始和结束 \是转义
    $pat = "/<span>名称：<\/span>发达国家模拟<br \/>/";
    preg_match($pat,$content,$matches);
    dump($matches);
}

public function index12(){
  echo'
  <pre>
    $content = file_get_contents("http://www.hyhthjk.com.cn/note.php/pachong/Index/index10");
    $matches = array();
    //要匹配的内容  斜杠 / /是匹配的开始和结束 \是转义
    $pat = "/<span>名称：<\/span>发达国家模拟<br\/>/";
    preg_match($pat,$content,$matches);
   </pre><hr/>
   ';
}

public function index13(){
    $this->index14();
    $content = file_get_contents("http://www.hyhthjk.com.cn/note.php/pachong/Index/index10");
    $matches = array();
    echo '校验';
    //要匹配的内容  斜杠 / /是匹配的开始和结束 \是转义
    //  （）是一个匹配的分组 ^除掉 * 0个或任意多个 [] 匹配的字符范围
    // $pat = "/<span>名称：<\/span>([^(<br\/>)]*)<br\/>/";  对
   //  $pat = "/<span>名称：<\/span>([^(<br)]*)<br\/>/"; 原
     $pat = "/<span>名称：<\/span><br\/>/";
    preg_match_all($pat,$content,$matches);
    dump($matches);
    //取[1]
    dump($matches['1']);
}

public function index14(){
    echo'
    <pre>
    $content = file_get_contents("http://www.hyhthjk.com.cn/note.php/pachong/Index/index10");
    $matches = array();
    //要匹配的内容  斜杠 / /是匹配的开始和结束 \是转义
    //  （）是一个匹配的分组 ^除掉 * 0个或任意多个 [] 匹配的字符范围
    $pat = "/<span>名称：<\/span>([^(<br)]*)<br\/>/";
    preg_match($pat,$content,$matches);
    dump($matches);
    //取[1]
    dump($matches["1"]);
    </pre>
    <br/>
    ';

}

public function index15(){
    $html=file_get_contents('https://www.sogou.com/sogou?query=gif压缩');
    $xml = simplexml_load_file($html);
    $result = $xml->xpath("from");
    dump($result);
     
    }


 /**模拟爬取表格  http://www.hyhthjk.com.cn/note.php/boot/Index/index12*/   
 // 案例 https://bbs.csdn.net/topics/390939564
 public function index16(){
   $arr = "<table>
   <thead>
       <tr>
           <th>编号</th>
           <th>姓名</th>
           <th>性别</th>
           <th>年龄</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>1</td>
           <td>张三</td>
           <td>男</td>
           <td>50</td>
       </tr>
       <tr >
           <td>2</td>
           <td>李四</td>
           <td>女</td>
           <td>48</td>
       </tr>
       <tr >
           <td>3</td>
           <td>王五</td>
           <td>男</td>
           <td>52</td>
       </tr>
       <tr>
           <td>4</td>
           <td>马六</td>
           <td>男</td>
           <td>55</td>
       </tr>
   </tbody>";
 
    $fcontents=file_get_contents("http://www.hyhthjk.com.cn/note.php/boot/Index/index12");  
    //dump($arr);
    $table_data = preg_match_all('/<table[^>]*>(.*?) <\/table>/', $fcontents,$match);
    dump($table_data);
    dump($match);
    exit();
    $table_data = $match[0][0];

    $table_array = explode('<tr>',$table_data);
    $data = array();

    for($i=2;$i<count($table_array);$i++){
        $data[$i] = explode('</td>',$table_array[$i]);
        for($j = 0;$j<count($data[$i]);$j++){
            $data[$i][$j] = preg_replace('/\s(?=\s)/','',trim(strip_tags($data[$i][$j])));
        }
            array_pop($data[$i]);
    }
    return $data;      


 }


 public function index17(){
   // $content = file_get_contents("http://vip.stock.finance.sina.com.cn/corp/go.php/vISSUE_ShareBonus/stockid/600690.phtml");
   // $content = file_get_contents("http://data.eastmoney.com/gpzy/pledgeRatio.aspx");  东方财富
   //  $content = file_get_contents("http://data.cnstock.com/gpsj/ggcg/603609.html");
     // $content = file_get_contents("http://www.kanguba.com/guquanzhiya/603609");
      $content = file_get_contents("http://data.cnstock.com/gpsj/ggcg/600600.html");
	if($content){
		$data = explode("~", $content);
		dump($data);
	}else{
  echo "异常";
	}
	// $content = "2019-09-03";
	 if(file_put_contents('../note/pachong/mylog2.log',$content."\n",FILE_APPEND)){
		echo '数据存入成功','<br />';
	}else{
		echo '数据存入失败','<br />';

  }
 }


   
}



